package ru.dc.gui.swing;

import ru.dc.Application;
import ru.dc.gui.swing.util.IconLoader;
import ru.dc.object.HubEventListener;
import ru.dc.object.ServerInfo;
import ru.dc.object.events.hub.HubEvent;
import ru.dc.object.events.hub.PrivateMessageRecieved;

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * Окно для личного чата с пользователем.
 *
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 11:53:52
 * </pre>
 */
public class PrivateChat extends Tab {
    private JTextArea chat;
    private JPanel rootPanel;
    private JTextField message;
    private JButton sendBtn;

    private ServerInfo hubInfo;
    private String remoteNick;
    private PrivateChat.AppendMessageListener listener = new AppendMessageListener();



    @Override
    public void init() {

        setIcon(IconLoader.getInstance().getIcon("user_icon.gif"));
        setTitle(remoteNick);

        sendBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        message.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "sendMessage");
        message.getActionMap().put("sendMessage", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });


        setLayout(new BorderLayout());
        add(rootPanel, BorderLayout.CENTER);
        Application.getApplication().getHubEventsTranslator().addEventListener(listener);
    }

    @Override
    public void close() {
        Application.getApplication().getHubEventsTranslator().removeEventListener(listener);
    }

    @Override
    public void onSelect() {
        super.onSelect();
        message.requestFocusInWindow();
    }

    public void appendMessage(String senderNick, String message) {
        chat.append(senderNick+": "+message+"\n");
        chat.scrollRectToVisible(new Rectangle(0, chat.getHeight()-2, 1, 1));
    }

    public void sendMessage() {
        final String message = this.message.getText();
        new Thread() {
            @Override
            public void run() {
                Application.getApplication().getConnectionManager().privateChat(hubInfo, remoteNick, message);
            }
        }.start();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                appendMessage(getHubInfo().getNick(), message);
                PrivateChat.this.message.setText("");
            }
        });
    }

    public String getRemoteNick() {
        return remoteNick;
    }

    public void setRemoteNick(String remoteNick) {
        this.remoteNick = remoteNick;
        setTitle(remoteNick);
    }

    public ServerInfo getHubInfo() {
        return hubInfo;
    }

    public void setHubInfo(ServerInfo hubInfo) {
        this.hubInfo = hubInfo;
    }

    private class AppendMessageListener implements HubEventListener {
        public void onEvent(HubEvent e) {
            if (!e.getServerInfo().equals(hubInfo)) {
                return;
            }

            if (!(e instanceof PrivateMessageRecieved)) {
                return;
            }

            PrivateMessageRecieved m = (PrivateMessageRecieved) e;
            if (m.getTargetNick().equals(getHubInfo().getNick()) && m.getSourceNick().equals(remoteNick)) {
                appendMessage(m.getSenderNick(), m.getMessage());
            }
        }
    }
}
